//
//  PPCameraCaptureConfirmViewController.m
//  pickpic
//
//  Created by Jinxiao on 10/7/16.
//  Copyright © 2016 MicroDream. All rights reserved.
//

#import "PPCameraCaptureConfirmViewController.h"
#import "PPCameraViewController.h"
#import "PPCameraCropViewController.h"

@interface PPCameraCaptureConfirmViewController () <TOCropViewControllerDelegate>
@property (readwrite, nonatomic, strong) UIImageView *previewView;
@property (readwrite, nonatomic, strong) DGAlignmentButton *retakeButton;
@property (readwrite, nonatomic, strong) UIButton *editButton;
@property (readwrite, nonatomic, strong) UIButton *confirmButton;
@end

@implementation PPCameraCaptureConfirmViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    self.view.backgroundColor = [UIColor blackColor];

    _previewView = [[UIImageView alloc] init];
    _previewView.image = _image;
    _previewView.backgroundColor = [UIColor clearColor];
    _previewView.contentMode = UIViewContentModeScaleAspectFit;
    [self.view addSubview:_previewView];

    _retakeButton = [DGAlignmentButton buttonWithType:UIButtonTypeCustom];
    _retakeButton.style = DGAlignmentButtonStyleImageLeft;
    _retakeButton.spacing = 16;
    _retakeButton.titleLabel.font = [UIFont systemFontOfSize:16];
    _retakeButton.contentEdgeInsets = UIEdgeInsetsMake(10, 10, 10, 10);
    [_retakeButton setTitle:@"重拍" forState:UIControlStateNormal];
    [_retakeButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [_retakeButton setImage:[UIImage imageNamed:@"return"] forState:UIControlStateNormal];
    [_retakeButton addTarget:self action:@selector(retakeButtonDidPressed) forControlEvents:UIControlEventTouchUpInside];
    [_retakeButton sizeToFit];

    UIBarButtonItem *backItem = [[UIBarButtonItem alloc] initWithCustomView:_retakeButton];

    UIBarButtonItem *negItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
    negItem.width = -14;

    self.navigationItem.leftBarButtonItems = @[negItem, backItem];

    _editButton = [UIButton buttonWithType:UIButtonTypeCustom];
    _editButton.contentEdgeInsets = UIEdgeInsetsMake(10, 10, 10, 10);
    _editButton.titleLabel.font = [UIFont systemFontOfSize:14];
    [_editButton setTitle:@"编辑" forState:UIControlStateNormal];
    [_confirmButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [_editButton addTarget:self action:@selector(editButtonDidPressed) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:_editButton];

    _confirmButton = [UIButton buttonWithType:UIButtonTypeCustom];
    _confirmButton.contentEdgeInsets = UIEdgeInsetsMake(10, 10, 10, 10);
    _confirmButton.titleLabel.font = [UIFont systemFontOfSize:14];
    [_confirmButton setTitle:@"保存" forState:UIControlStateNormal];
    [_confirmButton setTitleColor:[UIColor colorWithRGBHex:0xffdd17] forState:UIControlStateNormal];
    [_confirmButton addTarget:self action:@selector(confirmButtonDidPressed) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:_confirmButton];

    [_editButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.equalTo(self.view).offset(10);
        make.bottom.equalTo(self.view).offset(-40);
    }];

    [_confirmButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.trailing.equalTo(self.view).offset(-10);
        make.bottom.equalTo(self.view).offset(-40);
    }];

    [_previewView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.equalTo(self.view);
        make.height.and.width.lessThanOrEqualTo(self.view.mas_width).with.priorityHigh();
        make.height.and.width.lessThanOrEqualTo(self.view.mas_height).with.priorityHigh();
        make.height.and.width.equalTo(self.view.mas_width).with.priorityLow();
        make.height.and.width.equalTo(self.view.mas_height).with.priorityLow();
    }];
}

- (void)retakeButtonDidPressed {
    [MobClick event:@"28"];
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)editButtonDidPressed {
    [MobClick event:@"29"];
    PPCameraCropViewController *cropViewController = [[PPCameraCropViewController alloc] initWithCroppingStyle:TOCropViewCroppingStyleDefault image:_image];
    cropViewController.toolbar.cancelIconButton.alpha = 0;
    cropViewController.toolbar.cancelTextButton.alpha = 0;
    cropViewController.delegate = self;
    [self.navigationController pushViewController:cropViewController animated:YES];
}

- (void)confirmButtonDidPressed {
    [MobClick event:@"30"];
    PPCameraViewController *cameraViewController = (PPCameraViewController *)self.navigationController;

    if([cameraViewController.cameraDelegate respondsToSelector:@selector(cameraViewController:didSelectImages:)]) {
        [cameraViewController.cameraDelegate cameraViewController:cameraViewController didSelectImages:@[_image]];
    }
}

- (BOOL)prefersNavigationBarHidden {
    return NO;
}

#pragma mark - TOCropViewControllerDelegate

- (void)cropViewController:(TOCropViewController *)cropViewController didCropToImage:(UIImage *)image withRect:(CGRect)cropRect angle:(NSInteger)angle {
    [MobClick event:@"31"];
    PPCameraViewController *cameraViewController = (PPCameraViewController *)self.navigationController;

    if([cameraViewController.cameraDelegate respondsToSelector:@selector(cameraViewController:didSelectImages:)]) {
        [cameraViewController.cameraDelegate cameraViewController:cameraViewController didSelectImages:@[image]];
    }
}

- (void)cropViewController:(TOCropViewController *)cropViewController didFinishCancelled:(BOOL)cancelled {
    [self.navigationController popViewControllerAnimated:YES];
}

@end
